package com.bkgy.controller.qualityDaily.controller;

import com.bkgy.controller.qualityDaily.entity.QualitydailyMain;
import com.bkgy.controller.qualityDaily.entity.pdo.MainQueryReq;
import com.bkgy.controller.qualityDaily.entity.pdo.MainSaveReq;
import com.bkgy.controller.qualityDaily.entity.pdo.MainSaveTable;
import com.bkgy.controller.qualityDaily.entity.pdo.QualityReq;
import com.bkgy.controller.qualityDaily.service.IQualitydailyMainDataService;
import com.bkgy.controller.qualityDaily.service.IQualitydailyMainService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Tag(name = "质量日报-主体指标完成情况")
@Controller
@RequestMapping("/targetcomplete")
public class QualitydailyMainController {
    @Autowired
    private IQualitydailyMainService mainService;
    @Autowired
    private IQualitydailyMainDataService mainDataService;

    @ResponseBody
    @Operation(summary = "分页查询", description = "质量日报》质量填报》主体完成指标列表")
    @PostMapping("/getTargetcompletePage")
    public ResultData selectPage(@RequestBody MainQueryReq request) {
        return mainService.selectPage(request);
    }

    @ResponseBody
    @Operation(summary = "新增", description = "质量日报》质量填报》新增")
    @PostMapping("/targetcompleteadd")
    public ResultData<String> saveQualitydailyMain(@RequestBody @Valid MainSaveReq req) {
        QualitydailyMain obj = new QualitydailyMain();
        obj.setId(null);
        obj.setYearMonthDay(req.getYearMonthDay());
        obj.setArea(req.getArea());
        obj.setCreateTime(new Date());
        List<MainSaveTable> list = req.getMainTable();
        for (int i = 0; i < list.size(); i++) {
            MainSaveTable main = list.get(i);
            try {
                obj.getClass().getMethod("setCol" + i + 0, BigDecimal.class).invoke(obj, main.getOne());
                obj.getClass().getMethod("setCol" + i + 1, BigDecimal.class).invoke(obj, main.getTwo());
                obj.getClass().getMethod("setCol" + i + 2, BigDecimal.class).invoke(obj, main.getThree());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return mainService.saveQualitydailyMain(obj);
    }

    @ResponseBody
    @Operation(summary = "修改", description = "质量日报》质量填报》修改")
    @PostMapping("/targetcompleteupdate")
    public ResultData<String> updateQualitydailyMain(@RequestBody @Valid MainSaveReq req) {
        QualitydailyMain obj = new QualitydailyMain();
        obj.setId(req.getId());
        obj.setYearMonthDay(req.getYearMonthDay());
        obj.setArea(req.getArea());
        List<MainSaveTable> list = req.getMainTable();
        for (int i = 0; i < list.size(); i++) {
            MainSaveTable main = list.get(i);
            try {
                obj.getClass().getMethod("setCol" + i + 0, BigDecimal.class).invoke(obj, main.getOne());
                obj.getClass().getMethod("setCol" + i + 1, BigDecimal.class).invoke(obj, main.getTwo());
                obj.getClass().getMethod("setCol" + i + 2, BigDecimal.class).invoke(obj, main.getThree());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return mainService.updateQualitydailyMain(obj);
    }

    @ResponseBody
    @Operation(summary = "删除", description = "质量日报》质量填报》删除")
    @PostMapping("/targetcompletedel")
    public ResultData<String> delelteQualitydailyMain(@RequestBody MainSaveReq req) {
        QualitydailyMain obj = new QualitydailyMain();
        obj.setId(req.getId());
        obj.setYearMonthDay(req.getYearMonthDay());
        obj.setArea(req.getArea());
        return mainService.deleteQualitydailyMain(obj);
    }

    @ResponseBody
    @Operation(summary = "主体指标完成情况折线图", description = "质量日报》三冷区域》主体指标完成情况折线图")
    @PostMapping("/mainLineRep")
    public ResultData mainLineRep() {
        return mainService.mainLineRep();
    }

    @ResponseBody
    @Operation(summary = "主体指标完成情况报表", description = "质量日报》三冷区域》主体指标完成情况")
    @PostMapping("/mainRep")
    public ResultData mainRep(@RequestBody QualityReq obj) {
        obj.setDbegin(obj.getDbegin().replaceAll("-",""));
        obj.setDend(obj.getDend().replaceAll("-",""));
        return mainService.mainRep(obj);
    }

    @ResponseBody
    @Operation(summary = "【构建主体指标完成情况表数据】", description = "质量日报》三冷区域》构建主体指标完成情况表数据yyyyMMdd")
    @PostMapping("/buildMainRepData")
    public ResultData<String> buildMainRepData(String startDay, String endDay) {
        try {
            Date d1 = DateUtil.getStringToDate("yyyyMMdd", startDay);
            Date d2 = DateUtil.getStringToDate("yyyyMMdd", endDay);

            while (d1.before(d2)) {
                mainDataService.buildData(d1);
                d1 = DateUtil.getAfterDate(d1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataSuccess(e.getMessage());
        }
        return ResultData.ResultDataSuccess("SUCCESS");
    }
}